package com.estore.action;

import java.util.Map;

import com.estore.converter.DateTime;
import com.estore.converter.MD5;
import com.estore.info.AccountInfo;
import com.estore.info.ProfileInfo;
import com.estore.info.ShopInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.estore.manager.ProfileManager;
import com.estore.manager.ShopManager;
import com.estore.model.LoginModel;

public class LogInAction extends ActionSupport implements
		ModelDriven<LoginModel> {
	private static final long serialVersionUID = -7769137946345407512L;
	private LoginModel model = new LoginModel();
	private ProfileManager profileManager;
	private ShopManager shopManager;
	DateTime dateTime = new DateTime();

	public String login() {
		// List<ShowProfileInfo> listinfo = managerList.getListProfileInfo();
		AccountInfo infoAcc = profileManager.getAccountInfo(model.getId(),
				MD5.encryptMD5(model.getPassword()));
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (infoAcc != null) {
			ProfileInfo info = profileManager.getUserInfo(infoAcc.getUserID());
			info.setUserID(infoAcc.getUserID());
			session.put("logged-in", "true");
			session.put("userID", infoAcc.getUserID());
			session.put("role", info.getRole());
			session.put("isBanned", info.getIsBanned());
			session.put("username", info.getAccount());
			session.put("password", info.getPassword());
			session.put("firstName", info.getFirstName());
			session.put("lastName", info.getLastName());
			session.put("birthDay", info.getDateOfBirth());
			session.put("phoneNumber", info.getPhoneNumber());
			session.put("email", info.getEmail());
			session.put("address", info.getAddress());
			session.put("gentle", info.getSex());
			session.put("secretQuestion", info.getSecretQuestion());
			session.put("secretAnswer", info.getSecretAnswer());
			session.put("avatar", info.getAvatar());
			if (shopManager.checkAgentAvail(info.getUserID())) {
				session.put("shop", "Avail");
				if (shopManager.getShopInfo(infoAcc.getUserID()) != null) {
					ShopInfo shopInfo = shopManager.getShopInfo(infoAcc
							.getUserID());
					session.put("shopInfo", shopInfo);
				}
			} else {
				session.put("shop", "NotAvail");
			}
			return "success";
		} else {
			model.setErrorLogin("Sai Tài Khoản Hoặc Mật Khẩu");
			model.setId(null);
			return "error";
		}
	}

	public String logout() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.remove("logged-in");
		session.remove("userID");
		session.remove("username");
		session.remove("role");
		session.remove("isBanned");
		session.remove("password");
		session.remove("firstName");
		session.remove("lastName");
		session.remove("birthDay");
		session.remove("email");
		session.remove("address");
		session.remove("gentle");
		session.remove("phoneNumber");
		session.remove("secretQuestion");
		session.remove("secretAnswer");
		session.remove("avatar");
		session.remove("shop");
		session.remove("shopInfo");
		return "success";

	}

	@Override
	public LoginModel getModel() {
		return model;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setModel(LoginModel model) {
		this.model = model;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

	public DateTime getDateTime() {
		return dateTime;
	}

	public void setDateTime(DateTime dateTime) {
		this.dateTime = dateTime;
	}

	public ShopManager getShopManager() {
		return shopManager;
	}

	public void setShopManager(ShopManager shopManager) {
		this.shopManager = shopManager;
	}
}
